package com.autolinq.service;

import java.util.Date;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.autolinq.util.Constant;
import com.autolinq.util.MenuUtil;
import com.autolinq.util.SHA1;
import com.autolinq.util.http.HttpPostMessage;

import net.sf.json.JSONObject;

public class ServiceHandler{
	static Logger logger = Logger.getLogger("com.autolinq.service.ServiceHandler");
	public static String handle(String request,Properties prop){
		String response = "";
		JSONObject jsonObject = null;
		try {
			jsonObject = JSONObject.fromObject(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.debug("FunctionID--"+jsonObject.get("FunctionID")+":"+request);
		if(jsonObject!=null){
			if("ListFriends".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileListFriendsURL"), request);
			}else if("CreateTrip".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileCreateTripURL"), request);
			}else if("ListTrip".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileListTripURL"), request);
			}else if("TripDetail".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileTripDetailURL"), request);
			}else if("EditTrip".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileEditTripURL"), request);
			}else if("InviteFriend".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileInviteFriendURL"), request);
			}else if("UserDetail".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileUserDetailURL"), request);
			}else if("EditTripStatus".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileEditTripStatusURL"), request);
			}else if("CreatePickup".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileCreatePickupURL"), request);
			}else if("ListPickup".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileListPickupURL"), request);
			}else if("DynamicInfoTrip".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileDynamicInfoTripURL"), request);
			}else if("PickupDetail".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobilePickupDetailURL"), request);
			}else if("DynamicInfoPickup".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileDynamicInfoPickupURL"), request);
			}else if("UpdateStatusPickup".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileUpdateStatusPickupURL"), request);
			}else if("JSSignature".equals(jsonObject.get("FunctionID"))){
				String token,ticket;
				try {
//					response = HttpPostMessage.readContentFromGet(prop.getProperty("tokenURL"), request);
//					token = (JSONObject.fromObject(response)).getString("token");
//					//token = MenuUtil.getAccessToken("wxd9a94803cacf5b48", "c358ce24e3575168267b2ee643ddce27");
//					
//					logger.debug("token="+token);
//					ticket = MenuUtil.getTicket(token);
					
					response = HttpPostMessage.readContentFromGet(prop.getProperty("ticketURL"), request);
					ticket = (JSONObject.fromObject(response)).getString("ticket");
					
					String noncestr = "2wsx3edc";
					String timestamp = "";
					String timeLong = String.valueOf(new Date().getTime());
					timestamp = timeLong.substring(0, timeLong.length()-3);
					String url = ((JSONObject)((JSONObject)jsonObject.get("Message")).get("Data")).getString("PageURL");
					String signature = new SHA1().getDigestOfString(("jsapi_ticket="+ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url+"").getBytes());
					JSONObject jo = JSONObject.fromObject("{\"timestamp\":\""+timestamp+"\"}");
					jo.put("noncestr", noncestr);
					jo.put("signature", signature);
					jo.put("appId", prop.get("wechatAppID"));
					
					String json = "{\"AppID\":\"CoDriveTrip\"}";
					JSONObject jsonObj = JSONObject.fromObject(json);
					jsonObj.put("FunctionID", "JSSignatureResp");
					jsonObj.put("AppVersion", "V1.00");
					jsonObj.put("ErrorCode", 0);
					jsonObj.put("Data", jo);
					
					response = jsonObj.toString();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else if("JSToken".equals(jsonObject.get("FunctionID"))){
				String token,ticket;
				try {
					response = HttpPostMessage.readContentFromGet(prop.getProperty("tokenURL"), request);
					token = (JSONObject.fromObject(response)).getString("token");
					JSONObject jo = JSONObject.fromObject("{\"token\":\""+token+"\"}");
					
					String json = "{\"AppID\":\"CoDriveTrip\"}";
					JSONObject jsonObj = JSONObject.fromObject(json);
					jsonObj.put("FunctionID", "JSTokenResp");
					jsonObj.put("AppVersion", "V1.00");
					jsonObj.put("ErrorCode", 0);
					jsonObj.put("Data", jo);
					
					response = jsonObj.toString();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else if("UploadVoice".equals(jsonObject.get("FunctionID"))){
				try {
					String voiceId = ((JSONObject)((JSONObject)jsonObject.get("Message")).get("Data")).get("VoiceID").toString();
					logger.debug("voiceId="+voiceId);
					response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileUploadVoiceURL"), request);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else if("ListVoice".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileListVoiceURL"), request);
			}else if("CreateUser".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileCreateUserURL"), request);
			}else if("EditUser".equals(jsonObject.get("FunctionID"))){
				response = HttpPostMessage.postMessageUTF8(prop.getProperty("mobileEditUserUserURL"), request);
			}
			
		}
		return response;
	}

}